﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Billing.DataObjects;
using System.Linq;

namespace Billing.Transactions
{
    public partial class TransactionBase : Billing.WindowsBase.FormBase
    {
        public TransactionBase()
        {
            InitializeComponent();
        }
        BillingDataContext mBillingDataContext;
        public BillingDataContext BillingDataContext
        {
            get
            {
                if (mBillingDataContext == null)
                    mBillingDataContext = new BillingDataContext();
                return mBillingDataContext;
            }
            set { mBillingDataContext = value; }
        }
        public Line CurrentLine
        {
            get { return this.bsLines.Current as Line; }
        }

        public IEnumerable<Line> Lines
        {
            get { return this.bsLines.GetEnumerator() as IEnumerable<Line>; }
        }
        private Header mCurrentHeader;
        public Header CurrentHeader
        {
            get { return mCurrentHeader; }
            set { mCurrentHeader = value; }
        }

        public virtual void OnSetHeaderDefaults(Header header)
        {
            header.DocumentDate = DateTime.Now;
            header.AccountingDate = DateTime.Now;
            header.CreatorID =  global::Billing.Profile.UserID.Value;
            header.HeaderStatuse = (from i in this.BillingDataContext.HeaderStatuses
                                    where i.ID == 1
                                    select i).First();

        }
        public virtual void OnSetLineDefaults(Line line)
        {
            line.Header = this.CurrentHeader;
        }
        public virtual void OnDataLoad()
        {

        }

        private void TransactionBase_Load(object sender, EventArgs e)
        {
            this.OnDataLoad();

        }
        public void AddNewHeader()
        {
            this.CurrentHeader = new Billing.DataObjects.Header();
            this.bsHeader.DataSource = this.CurrentHeader;
            this.BillingDataContext.Headers.InsertOnSubmit(this.CurrentHeader);
            this.OnSetHeaderDefaults(this.CurrentHeader);
        }
        public void EditHeader(long ID)
        {
            this.CurrentHeader = (from i in this.BillingDataContext.Headers
                                  where i.ID == ID
                                  select i).First();
            this.bsHeader.DataSource = this.CurrentHeader;
        }
        public void AddNewLine()
        {
            this.bsLines.DataSource = new Billing.DataObjects.Line();
            this.OnSetLineDefaults(this.CurrentLine);
            this.BillingDataContext.Lines.InsertOnSubmit(this.CurrentLine);

        }

        private void btnSave_Click(object sender, EventArgs e)
        {

            this.OnSave();
        }
        public virtual void OnSave()
        {
            try
            {
                this.bsHeader.EndEdit();
                this.bsLines.EndEdit();
                this.BillingDataContext.SubmitChanges();
               // this.BillingDataContext.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, this.BillingDataContext.Headers);

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
